
class TLector:
	def __init__(self):
		self.__data = {}
		self.__org = ''
		self.__data = {}

	def setOrigen(self, org):
		from commands import getoutput
		self.__org = org
		if self.__org[:5] == '"/dev' or 'UDF filesystem data' in getoutput('file '+self.__org):
			self.__isDvd = True	
		else:
			self.__isDvd = False	

	def addProgram(self, prog, flags, dvdCmd, crop):
		''' 
			prog : program name
			flags : option for prog to get info
			dvdCmd : option to read dvd
			crop : to get cropping information
		'''
		self.__data[prog] = {'flags':flags, 'dvdCmd':dvdCmd, 'crop':crop}

	def getCropData(self, title, time):
		from commands import getoutput
		print "Getting crop from title:", title, "with length:", time
		tmpData = ''
		for prog in self.__data.keys():
			for ss in [int(time)/6, int(time)/3, int(time)/1.7, int(time)/1.3]:
				cmd = prog
				cmd += " " + self.__data[prog]['flags']
				if self.__isDvd:
					cmd += ' ' + self.__data[prog]['dvdCmd']
					if not self.__data[prog]['dvdCmd']:
						cmd = 'echo skiping:' + prog 
				else:
					cmd +=  ' ' + self.__org
				if self.__data[prog]['crop']:
					cmd += ' ' + self.__data[prog]['crop']
					cmd += ' -ss ' + str(ss)
					print "## lector ##",cmd
					tmpData += getoutput(cmd)
		return tmpData
		
	def getRawData(self, title):
		from commands import getoutput
		tmpData = ''
		for prog in self.__data.keys():
			cmd = prog
			cmd += " " + self.__data[prog]['flags']
			if self.__isDvd:
				cmd += " " + self.__data[prog]['dvdCmd']
				if not self.__data[prog]['dvdCmd']:
					cmd = 'echo skiping:' + prog 
			else:
				cmd += " " + self.__org
			print "## lector ##",cmd
			tmpData += getoutput(cmd)
		return tmpData
	
if __name__ == "__main__":
	from sys import argv
	l = TLector()
	l.setOrigen(argv[1])
	l.addProgram('mplayer', '-v -nosound -identify -frames 1 -vo null -ao null', '-dvd-device ' + argv[1] + ' dvd://1', '-vo null -ao null -vf cropdetect -idx -nosound -frames 25 -speed 100')
	l.addProgram('ffmpeg', '-i', '', '')
	l.addProgram('mkvmerge', '-i', '', '')
	print l.getRawData(1)
	print l.getCrop(1, 123)
